/**
 * file
 */
package com.tdkj.action.backstage.website;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Properties;

import org.apache.commons.lang.StringUtils;

import com.tdkj.action.BaseAction;

import freemarker.template.Configuration;

/**
 * 
 * FileName: EmailSetAction.java
 * 
 * @Description: TODO(用一句话描述该文件做什么)
 * 
 *               All rights Reserved, Designed By ZTE-ITS
 * 
 *               Copyright: Copyright(C) 2010-2011
 * 
 *               Company 天成科技有限公司
 * 
 * @author: 李兴建
 * 
 * @version V1.0
 * 
 *          Createdate: 2012-9-5 下午08:16:23
 * 
 * 
 * 
 *          Modification History:
 * 
 *          Date Author Version Discription
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 *          
 *          ----------------------------------------------------------------------
 *          -------------
 * 
 *          2012-9-5 李兴建 1.0 1.0
 * 
 *          Why & What is modified: <修改原因描述>
 */
public class EmailSetAction extends BaseAction {
	private String subject;
	private String host;
	private String addr;
	private String name;
	private String pwd;

	private static final long serialVersionUID = -253242613153218521L;

	public String emailSetPage() throws Exception {
		Properties prop = new Properties();// 属性集合对象
		String filePath = Configuration.class.getResource("/").getPath() + "WebSiteConfig.properties";
		if (filePath.contains("%20")) {
			filePath = filePath.replaceAll("%20", " ");
		}
		if (filePath.startsWith("/")) {
			filePath = filePath.replaceFirst("/", "");
		}
		FileInputStream fis = new FileInputStream(filePath);// 属性文件输入流
		prop.load(fis);// 将属性文件流装载到Properties对象中

		//subject = prop.getProperty("Email_Config_subject");
		host = prop.getProperty("Email_Config_host");
		addr = prop.getProperty("Email_Config_addr");
		name = prop.getProperty("Email_Config_name");
		pwd = prop.getProperty("Email_Config_password");
		// fis.close();// 关闭流
		return "emailPage";
	}

	public String emailSave() throws Exception {
		boolean tag = true;
		/*if (StringUtils.isEmpty(subject)) {
			this.addFieldError("subjectError", "显示名称不能为空");
			tag = false;
		}*/
		if (StringUtils.isEmpty(host)) {
			this.addFieldError("hostError", "协议名称不能为空");
			tag = false;
		}
		if (StringUtils.isEmpty(addr)) {
			this.addFieldError("addrError", "邮箱地址不能为空");
			tag = false;
		}
		if (StringUtils.isEmpty(name)) {
			this.addFieldError("nameError", "邮箱用户名不能为空");
			tag = false;
		}
		if (StringUtils.isEmpty(pwd)) {
			this.addFieldError("pwdError", "密码不能为空");
			tag = false;
		}
		if (!tag) {
			return emailSetPage();
		}
		String filePath = Configuration.class.getResource("/").getPath() + "WebSiteConfig.properties";

		if (filePath.contains("%20")) {
			filePath = filePath.replaceAll("%20", " ");
		}
		if (filePath.startsWith("/")) {
			filePath = filePath.replaceFirst("/", "");
		}
		FileInputStream fis = new FileInputStream(filePath);// 属性文件输入流
		Properties prop = new Properties();// 属性集合对象
		prop.load(fis);// 将属性文件流装载到Properties对象中
		fis.close();// 关闭流
		// System.out.println(filePath);

		FileOutputStream fos = new FileOutputStream(filePath);
		// 将Properties集合保存到流中
		//prop.setProperty("Email_Config_subject", subject);
		prop.setProperty("Email_Config_host", host);
		prop.setProperty("Email_Config_addr", addr);
		prop.setProperty("Email_Config_name", name);
		prop.setProperty("Email_Config_password", pwd);

		prop.store(fos, "Copyright (c) Boxcode Studio");
		fos.close();// 关闭流
		this.addFieldError("infoError", "保存成功");
		return emailSetPage();
	}

	/**
	 * @return the subject
	 */
	public String getSubject() {
		return subject;
	}

	/**
	 * @param subject
	 *            the subject to set
	 */
	public void setSubject(String subject) {
		this.subject = subject;
	}

	/**
	 * @return the host
	 */
	public String getHost() {
		return host;
	}

	/**
	 * @param host
	 *            the host to set
	 */
	public void setHost(String host) {
		this.host = host;
	}

	/**
	 * @return the addr
	 */
	public String getAddr() {
		return addr;
	}

	/**
	 * @param addr
	 *            the addr to set
	 */
	public void setAddr(String addr) {
		this.addr = addr;
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name
	 *            the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @return the pwd
	 */
	public String getPwd() {
		return pwd;
	}

	/**
	 * @param pwd
	 *            the pwd to set
	 */
	public void setPwd(String pwd) {
		this.pwd = pwd;
	}

	/**
	 * @return the serialversionuid
	 */
	public static long getSerialversionuid() {
		return serialVersionUID;
	}

}
